<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/7/4
 * Time: 17:22
 */

namespace App\Process;


use App\Utility\BaseEnum;
use EasySwoole\Component\Process\AbstractProcess;
use App\Common\SumComm;
use Swoole\Timer;
use App\Common\PorcessComm;

/**
 * TODO 公共的处理线程类(定时器为每分钟执行一次)；
 * 
 * @author Administrator
 */
class CommProcess extends AbstractProcess
{
    private $everyMinute = 1;           //轮循时间间隔三分钟
    public function run($arg) {
        $timeStamp = $this->everyMinute * 60 * 1000;
        $funLists = PorcessComm::PORCESS_START_FUNCTIONS; //要执行的函数列表
        BaseEnum::Logger('执行公共异步线程：启动函数及参数 =>  '.json_encode($arg) ,'user-login');
        
        //TODO 启动定时器
        $this->addTick($timeStamp , function() use ($funLists){
            BaseEnum::Logger("执行线程定时操作：========>PorcessComm AddTick=======>".json_encode($funLists),'user-login');
            if (!empty($funLists)) {
                foreach ($funLists as $oneFun => $oneEveryTime){
                    if (!empty($oneFun) && method_exists(PorcessComm::class,$oneFun)) {
                        
                        //#####线程处理：调用static静态方法完成 ######
                        @call_user_func_array(array("App\Common\PorcessComm" , $oneFun),array($oneEveryTime));
                        
                    }else{
                        BaseEnum::Logger("执行公共异步线程失败：========>PorcessComm :->  {$oneFun}=======>函数未定义  ",'user-login');
                    }
                }
            }else{
                BaseEnum::Logger('执行公共异步线程结果：========>没有可执行的线程=======>  '.json_encode($funLists) ,'user-login');
            }
        });
        
        Timer::after(5000, function(){
            SumComm::sumALLAgentLineData();                     //#####第一次就调用######所有代理的运营数据
        });
    }
    

    public function onShutDown()
    {
        // TODO: Implement onShutDown() method.
    }

    public function onReceive(string $str, ...$args)
    {
        // TODO: Implement onReceive() method.
    }

}